<template>
  <div class="delete-page">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>商品列表</span>
          <el-button @click="$router.back()" size="small">返回</el-button>
        </div>
      </template>
      
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="商品名称" />
        <el-table-column label="商品图片" width="180">
          <template #default="scope">
            <img :src="'http://localhost:3005' + scope.row.image" class="product-image" alt="商品图片">
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button
              type="danger"
              size="small"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 删除确认对话框 -->
    <el-dialog
      v-model="deleteDialogVisible"
      title="确认删除"
      width="30%"
    >
      <span>确定要删除该商品吗？此操作不可恢复。</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button type="danger" @click="confirmDelete" :loading="deleting">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

// 表格数据
const tableData = ref([])

// 获取商品列表
const fetchItems = async () => {
  try {
    const response = await axios.post('http://localhost:3005/cart/list')
    if (response.data.success === "成功") {
      tableData.value = response.data.result
    } else {
      ElMessage({
        type: 'error',
        message: '获取商品列表失败：' + response.data.msg
      })
    }
  } catch (error) {
    ElMessage({
      type: 'error',
      message: '获取商品列表失败：' + error.message
    })
  }
}

// 在组件挂载时获取商品列表
fetchItems()

// 删除对话框显示状态
const deleteDialogVisible = ref(false)
// 当前要删除的商品
const currentItem = ref(null)
// 删除加载状态
const deleting = ref(false)

// 点击删除按钮
const handleDelete = (row) => {
  currentItem.value = row
  deleteDialogVisible.value = true
}

// 确认删除
const confirmDelete = async () => {
  try {
    deleting.value = true
    await axios.post('http://localhost:3005/cart/delete', {
      id: currentItem.value.id
    })
    
    const index = tableData.value.findIndex(item => item.id === currentItem.value.id)
    if (index > -1) {
      tableData.value.splice(index, 1)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
    }
  } catch (error) {
    ElMessage({
      type: 'error',
      message: '删除失败：' + error.message
    })
  } finally {
    deleting.value = false
    deleteDialogVisible.value = false
  }
}
</script>

<style scoped>
.delete-page {
  padding: 20px;
  margin-left: -220px;
  width: 830px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.product-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
}
</style>